import { ChatOllama } from "@langchain/ollama";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { RunnableLambda, RunnableSequence } from "@langchain/core/runnables";

// 1. 创建 Prompt 模板
const pt = PromptTemplate.fromTemplate("请使用中文解释以下概念：{topic}");

// 2. 创建本地模型
const model = new ChatOllama({ model: "llama3", temperature: 0.7 });

// 3. 创建字符串解析器
const parser = new StringOutputParser();

// 4. 插件
const fn = (text) => {
  return text.replace(/闭包/g, "*&*闭包*&*");
};
const highlight = RunnableLambda.from(fn);

const chain = RunnableSequence.from([pt, model, parser, highlight]);

const res = await chain.invoke({
  topic: "闭包",
});

console.log(res);
